package com.complex.client.ui;

import com.complex.client.service.gen.WebServiceTreeNode;

import javax.swing.event.EventListenerList;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.ArrayList;
import java.util.EventListener;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: AGrankin
 * Date: 27.08.13
 * Time: 16:22
 * To change this template use File | Settings | File Templates.
 */
public class TreeNodesModel implements TreeModel {

    private EventListenerList listenerList = new EventListenerList();

    private List<WebServiceTreeNode> nodesList = null;

//    public TreeNodesModel(List<WebServiceTreeNode> nodesList) {
//        this.nodesList = nodesList;
//    }

    public void setData(List<WebServiceTreeNode> nodesList) {
        this.nodesList = nodesList;
        fireTreeStructureChanged(getRoot());
    }

    @Override
    public Object getRoot() {
        if(nodesList != null) {
            return nodesList.get(0);
        } else {
            return "default";
        }
    }

    @Override
    public Object getChild(Object parent, int index) {
        List<WebServiceTreeNode> children = new ArrayList<WebServiceTreeNode>();
        for(int i = 1; i < nodesList.size(); i++) {
            if(nodesList.get(i).getParentID() == ((WebServiceTreeNode)parent).getNodeID()) {
                children.add(nodesList.get(i));
            }
        }
        return children.get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        int count = 0;
        for(int i = 1; i < nodesList.size(); i++) {
            if(nodesList.get(i).getParentID() == ((WebServiceTreeNode)parent).getNodeID()) {
                count++;
            }
        }
        return count;
    }

    @Override
    public boolean isLeaf(Object node) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void valueForPathChanged(TreePath path, Object newValue) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        return 0;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void addTreeModelListener(TreeModelListener l) {
       listenerList.add(TreeModelListener.class, l);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener l) {
        listenerList.remove(TreeModelListener.class, l);
    }

    protected void fireTreeStructureChanged(Object oldRoot) {
        TreeModelEvent event = new TreeModelEvent(this, new Object[] {oldRoot});
        EventListener[] listeners = listenerList.getListeners(TreeModelListener.class);
        for (int i = 0; i<listeners.length; i++) {
            ((TreeModelListener)listeners[i]).treeStructureChanged(event);
        }
    }
}
